<template>
  <div class="login-box">
    <div class="login-content">
      <div class="imgbox">
        <img
          src="../assets/logo.png"
          alt=""
        >
      </div>
      <el-form
        class="from_input"
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginFormRules"
        label-width="0px"
      >
        <el-form-item prop='username'>
          <el-input
            v-model="loginForm.username"
            prefix-icon="el-icon-user-solid"
          ></el-input>
        </el-form-item>
        <el-form-item prop='password'>
          <el-input
            v-model="loginForm.password"
            prefix-icon='el-icon-lock'
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            class="submit"
            @click="login"
          >登录</el-button>
          <el-button
            @click="resetLoginForm"
            type="warning"
            class="reset"
          >重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 这时登录表单的数据绑定对象
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 这时表单的验证规则对象
      loginFormRules: {
        // 验证用户名是否合法(required)是否必填
        // 验证规则给input的父itme加prop=
        username: [
          { required: true, message: '请输入账户', trigger: 'blur' },
          { min: 3, max: 12, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: '请输入登陆密码', trigger: 'blur' },
          { min: 6, max: 14, message: '长度在 6 到 14 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetLoginForm() {
      // console.log(this) // 通过绑定ref拿到表单的实例对象
      // loginFormRef表单的引用对象
      this.$refs.loginFormRef.resetFields()// resetFields重置的方法
    },
    login() {
      // 验证规则的判定
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('/login', this.loginForm)
        // 通过layui弹出框
        if (res.meta.status !== 200) {
          return this.$message.error('登录失败');
        }
        this.$message.success('登陆成功')
        // 将token保存到sessionStorage
        sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.login-box {
  height: 100%;
  background-color: #2b4b6b;
  .login-content {
    width: 450px;
    height: 300px;
    background-color: #fff;
    // margin: 100px auto;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }
  .imgbox {
    width: 120px;
    height: 120px;
    border-radius: 50%;
    position: absolute;
    left: 50%;
    transform: translate(-50%);
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
    }
  }
  .from_input {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }
}
</style>
